using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ResourceBlender_Express {
    /// <summary>
    /// A nicer looking message dialog
    /// </summary>
    public partial class DialogBox : Window {
        /// <summary>
        /// The result returned by a DialogBox
        /// </summary>
        public enum DialogBoxButton {
            OK,
            Yes,
            YesToAll,
            No,
            NoToAll,
            Cancel,
            Abort,
            Retry,
            Ignore
        }

        /// <summary>
        /// Icons usable by the DialogBox
        /// </summary>
        public enum DialogBoxIcon {
            Error,
            Warning,
            Information,
            Question
        }

        /// <summary>
        /// Types of DialogBox
        /// </summary>
        public enum DialogBoxType {
            OK,
            OKCancel,
            YesNo,
            YesNoAll,
            YesNoCancel,
            AbortRetryCancel
        }

        private DialogBoxButton result;
        /// <summary>
        /// Gets the button which the user clicked.
        /// </summary>
        /// <value>The result.</value>
        public DialogBoxButton Result {
            get { return result; }
        }

        /// <summary>
        /// Initializes a new <see cref="DialogBox"/>.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="defaultResult">The default button.</param>
        private DialogBox(Window owner, string messageText, string caption, DialogBoxType type, DialogBoxIcon icon, DialogBoxButton defaultResult) {
            InitializeComponent();
            this.Owner = owner;
            this.Title = caption;
            txtMessage.Text = messageText;
            switch(type) {
                case DialogBoxType.OK:
                    btnOK.Visibility = Visibility.Visible;
                    btnOK.IsDefault = true;
                    break;
                case DialogBoxType.OKCancel:
                    btnOK.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Visible;
                    if(defaultResult == DialogBoxButton.Cancel) {
                        btnCancel.IsDefault = true;
                    } else {
                        btnOK.IsDefault = true;
                    }
                    break;
                case DialogBoxType.YesNo:
                    btnYes.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    if(defaultResult == DialogBoxButton.No) {
                        btnNo.IsDefault = true;
                    } else {
                        btnYes.IsDefault = true;
                    }
                    break;
                case DialogBoxType.YesNoAll:
                    btnYes.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    btnYesAll.Visibility = Visibility.Visible;
                    btnNoAll.Visibility = Visibility.Visible;
                    if(defaultResult == DialogBoxButton.No) {
                        btnNo.IsDefault = true;
                    } else if(defaultResult == DialogBoxButton.NoToAll) {
                        btnNoAll.IsDefault = true;
                    } else if(defaultResult == DialogBoxButton.YesToAll) {
                        btnYesAll.IsDefault = true;
                    } else {
                        btnYes.IsDefault = true;
                    }
                    break;
                case DialogBoxType.YesNoCancel:
                    btnYes.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Visible;
                    if(defaultResult == DialogBoxButton.Cancel) {
                        btnCancel.IsDefault = true;
                    } else if(defaultResult == DialogBoxButton.No) {
                        btnNo.IsDefault = true;
                    } else {
                        btnYes.IsDefault = true;
                    }
                    break;
                case DialogBoxType.AbortRetryCancel:
                    btnAbort.Visibility = Visibility.Visible;
                    btnRetry.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Visible;
                    if(defaultResult == DialogBoxButton.Abort) {
                        btnAbort.IsDefault = true;
                    } else if(defaultResult == DialogBoxButton.Retry) {
                        btnRetry.IsDefault = true;
                    } else {
                        btnRetry.IsDefault = true;
                    }
                    break;
            }
            switch(icon) {
                case DialogBoxIcon.Error:
                    this.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("pack://application:,,/Resources/images/error.ico", UriKind.RelativeOrAbsolute)).Stream);
                    img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/images/error_48.png", UriKind.RelativeOrAbsolute));
                    break;
                case DialogBoxIcon.Information:
                    this.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("pack://application:,,/Resources/images/info.ico", UriKind.RelativeOrAbsolute)).Stream);
                    img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/images/info_48.png", UriKind.RelativeOrAbsolute));
                    break;
                case DialogBoxIcon.Question:
                    this.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("pack://application:,,/Resources/images/question.ico", UriKind.RelativeOrAbsolute)).Stream);
                    img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/images/question_48.png", UriKind.RelativeOrAbsolute));
                    break;
                case DialogBoxIcon.Warning:
                    this.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("pack://application:,,/Resources/images/warning.ico", UriKind.RelativeOrAbsolute)).Stream);
                    img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/images/warning_48.png", UriKind.RelativeOrAbsolute));
                    break;
            }
        }

        /// <summary>
        /// Accepts and closes the dialog.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void AcceptDialog(object sender, ExecutedRoutedEventArgs e) {
            result = (DialogBoxButton)e.Parameter;
            DialogResult = true;
            Close();
        }

        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(Window owner, string messageText) {
            return DialogBox.Show(owner, messageText, "Information", DialogBoxType.OK, DialogBoxIcon.Information, DialogBoxButton.OK);
        }

        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(Window owner, string messageText, string caption) {
            return DialogBox.Show(owner, messageText, caption, DialogBoxType.OK, DialogBoxIcon.Information, DialogBoxButton.OK);
        }

        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(Window owner, string messageText, string caption, DialogBoxType type) {
            switch(type) {
                case DialogBoxType.OK:
                case DialogBoxType.OKCancel:
                    return DialogBox.Show(owner, messageText, caption, type, DialogBoxIcon.Information, DialogBoxButton.Retry);
                case DialogBoxType.YesNo:
                case DialogBoxType.YesNoAll:
                case DialogBoxType.YesNoCancel:
                    return DialogBox.Show(owner, messageText, caption, type, DialogBoxIcon.Question, DialogBoxButton.Retry);
                case DialogBoxType.AbortRetryCancel:
                default:
                    return DialogBox.Show(owner, messageText, caption, type, DialogBoxIcon.Question, DialogBoxButton.Retry);
            }
        }

        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <param name="icon">The icon.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(Window owner, string messageText, string caption, DialogBoxType type, DialogBoxIcon icon) {
            switch(type) {
                case DialogBoxType.OK:
                case DialogBoxType.OKCancel:
                    return DialogBox.Show(owner, messageText, caption, type, icon, DialogBoxButton.OK);
                case DialogBoxType.YesNo:
                case DialogBoxType.YesNoAll:
                case DialogBoxType.YesNoCancel:
                    return DialogBox.Show(owner, messageText, caption, type, icon, DialogBoxButton.Yes);
                case DialogBoxType.AbortRetryCancel:
                default:
                    return DialogBox.Show(owner, messageText, caption, type, icon, DialogBoxButton.Retry);
            }
        }

        /// <summary>
        /// Shows the specified owner.
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="defaultResult">The default result.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(Window owner, string messageText, string caption, DialogBoxType type, DialogBoxIcon icon, DialogBoxButton defaultResult) {
            DialogBox dlg = new DialogBox(owner, messageText, caption, type, icon, defaultResult);
            dlg.ShowDialog();
            return dlg.Result;
        }

        /// <summary>
        /// A shortcut method for showing an error message as an error with just an OK button
        /// </summary>
        /// <param name="owner">The owner window.</param>
        /// <param name="messageText">The message text.</param>
        /// <param name="caption">The caption.</param>
        /// <returns></returns>
        public static DialogBoxButton ShowAlert(Window owner, string messageText, string caption) {
            DialogBox dlg = new DialogBox(owner, messageText, caption, DialogBoxType.OK, DialogBoxIcon.Error, DialogBoxButton.OK);
            dlg.ShowDialog();
            return dlg.Result;
        }

        /// <summary>
        /// Shows the specified message text.
        /// </summary>
        /// <param name="messageText">The text to display.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(string messageText) {
            return DialogBox.Show(null, messageText, "Information", DialogBoxType.OK, DialogBoxIcon.Information, DialogBoxButton.OK);
        }

        /// <summary>
        /// Shows the specified message text.
        /// </summary>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(string messageText, string caption) {
            return DialogBox.Show(null, messageText, caption, DialogBoxType.OK, DialogBoxIcon.Information, DialogBoxButton.OK);
        }

        /// <summary>
        /// Shows the specified message text.
        /// </summary>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(string messageText, string caption, DialogBoxType type) {
            switch(type) {
                case DialogBoxType.OK:
                case DialogBoxType.OKCancel:
                    return DialogBox.Show(null, messageText, caption, type, DialogBoxIcon.Information, DialogBoxButton.Retry);
                case DialogBoxType.YesNo:
                case DialogBoxType.YesNoAll:
                case DialogBoxType.YesNoCancel:
                    return DialogBox.Show(null, messageText, caption, type, DialogBoxIcon.Question, DialogBoxButton.Retry);
                case DialogBoxType.AbortRetryCancel:
                default:
                    return DialogBox.Show(null, messageText, caption, type, DialogBoxIcon.Question, DialogBoxButton.Retry);
            }
        }

        /// <summary>
        /// Shows the specified message text.
        /// </summary>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <param name="icon">The icon.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(string messageText, string caption, DialogBoxType type, DialogBoxIcon icon) {
            switch(type) {
                case DialogBoxType.OK:
                case DialogBoxType.OKCancel:
                    return DialogBox.Show(null, messageText, caption, type, icon, DialogBoxButton.OK);
                case DialogBoxType.YesNo:
                case DialogBoxType.YesNoAll:
                case DialogBoxType.YesNoCancel:
                    return DialogBox.Show(null, messageText, caption, type, icon, DialogBoxButton.Yes);

                case DialogBoxType.AbortRetryCancel:
                default:
                    return DialogBox.Show(null, messageText, caption, type, icon, DialogBoxButton.Retry);
            }
        }

        /// <summary>
        /// Shows the specified message text.
        /// </summary>
        /// <param name="messageText">The text to display.</param>
        /// <param name="caption">The window caption.</param>
        /// <param name="type">The type of the message.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="defaultResult">The default result.</param>
        /// <returns></returns>
        public static DialogBoxButton Show(string messageText, string caption, DialogBoxType type, DialogBoxIcon icon, DialogBoxButton defaultResult) {
            return DialogBox.Show(null, messageText, caption, type, icon, defaultResult);
        }
    }
}
